package com.strange.mdl.module.mdl.controller.admin.videoinfo;

import cn.hutool.core.io.IoUtil;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.strange.mdl.framework.common.pojo.PageParam;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.pojo.CommonResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;
import static com.strange.mdl.framework.common.pojo.CommonResult.success;

import com.strange.mdl.framework.excel.core.util.ExcelUtils;

import com.strange.mdl.framework.apilog.core.annotation.ApiAccessLog;
import static com.strange.mdl.framework.apilog.core.enums.OperateTypeEnum.*;

import com.strange.mdl.module.mdl.controller.admin.videoinfo.vo.*;
import com.strange.mdl.module.mdl.dal.dataobject.videoinfo.MdlVideoInfoDO;
import com.strange.mdl.module.mdl.service.videoinfo.MdlVideoInfoService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 视频信息")
@RestController
@RequestMapping("/mdl/video-info")
@Validated
public class MdlVideoInfoController {

    @Resource
    private MdlVideoInfoService videoInfoService;


    // 上传视频
    @PostMapping("/upload-video")
    @Operation(summary = "上传视频")
    @PreAuthorize("@ss.hasPermission('mdl:video-info:upload')")
    public CommonResult<String> uploadVideoInfo(MdlVideoUploadReqVO videoUploadReqVO) {
        return success(videoInfoService.uploadVideo(videoUploadReqVO));
    }

    @PostMapping("/create")
    @Operation(summary = "创建视频信息")
    @PreAuthorize("@ss.hasPermission('mdl:video-info:create')")
    public CommonResult<Long> createVideoInfo(@Valid @RequestBody MdlVideoInfoSaveReqVO createReqVO) {
        return success(videoInfoService.createVideoInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新视频信息")
    @PreAuthorize("@ss.hasPermission('mdl:video-info:update')")
    public CommonResult<Boolean> updateVideoInfo(@Valid @RequestBody MdlVideoInfoSaveReqVO updateReqVO) {
        videoInfoService.updateVideoInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除视频信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdl:video-info:delete')")
    public CommonResult<Boolean> deleteVideoInfo(@RequestParam("id") Long id) {
        videoInfoService.deleteVideoInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除视频信息")
                @PreAuthorize("@ss.hasPermission('mdl:video-info:delete')")
    public CommonResult<Boolean> deleteVideoInfoList(@RequestParam("ids") List<Long> ids) {
        videoInfoService.deleteVideoInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得视频信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdl:video-info:query')")
    public CommonResult<MdlVideoInfoRespVO> getVideoInfo(@RequestParam("id") Long id) {
        MdlVideoInfoDO videoInfo = videoInfoService.getVideoInfo(id);
        return success(BeanUtils.toBean(videoInfo, MdlVideoInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得视频信息分页")
    @PreAuthorize("@ss.hasPermission('mdl:video-info:query')")
    public CommonResult<PageResult<MdlVideoInfoRespVO>> getVideoInfoPage(@Valid MdlVideoInfoPageReqVO pageReqVO) {
        PageResult<MdlVideoInfoDO> pageResult = videoInfoService.getVideoInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MdlVideoInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出视频信息 Excel")
    @PreAuthorize("@ss.hasPermission('mdl:video-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVideoInfoExcel(@Valid MdlVideoInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MdlVideoInfoDO> list = videoInfoService.getVideoInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "视频信息.xls", "数据", MdlVideoInfoRespVO.class,
                        BeanUtils.toBean(list, MdlVideoInfoRespVO.class));
    }

}